﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
	/// <summary>
	/// role manager
	/// </summary>
	public class RoleManager : LogicBase
    {
        

		private CategoryManager m_CategoryManager	= new CategoryManager();

		/// <summary>
		/// Role Type
		/// </summary>
		public enum ERole
		{
			Admin,
			//TopicManager,
			//Blog,
            Product
		}




		/// <summary>
		/// Insert a new UserRole into database
		/// </summary>
		/// <param name="aUserRole">new UserRole</param>
		public void Insert(UserRole aUserRole)
		{
			m_CosmeticDC.UserRoles.InsertOnSubmit(aUserRole);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete UserRole
		/// </summary>
		/// <param name="aUserRole">The UserRole want delete</param>
		public void Delete(UserRole aUserRole)
		{
			m_CosmeticDC.UserRoles.DeleteOnSubmit(aUserRole);
			m_CosmeticDC.SubmitChanges();
		}



		/// <summary>
		/// get role category by category name, if not find the category, create it.
		/// </summary>
		/// <param name="aRoleCategory">role category name</param>
		/// <returns>role category</returns>
		public Category GetRoleCategoryByName(ERole aRoleCategory)
		{
			Category theRoleTopCategory		= this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Role);
			List<Category> theCategoryList	= (from theCategory in m_CosmeticDC.Categories
												where theCategory.SupCategoryId == theRoleTopCategory.CategoryId && theCategory.Name == aRoleCategory.ToString()
												select theCategory).ToList();
			if (theCategoryList.Count == 0)
			{
				this.CheckRoleCategory();
				return this.GetRoleCategoryByName(aRoleCategory);
			}
			return theCategoryList[0];
		}
		
		/// <summary>
		/// check Role category whether or not exists. if not exists, create it.
		/// </summary>
		public void CheckRoleCategory()
		{
			Category theRoleTopCategory	= this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Role);
			Type theRoleType			= typeof(ERole);
			foreach (string theRoleName in Enum.GetNames(theRoleType))
			{
				ERole theRoleEnum				= (ERole)Enum.Parse(typeof(ERole), theRoleName);
				List<Category> theCategoryList	= (from theCategory in m_CosmeticDC.Categories
												where theCategory.SupCategoryId == theRoleTopCategory.CategoryId && theCategory.Name == theRoleEnum.ToString()
												select theCategory).ToList();
				if (theCategoryList.Count == 0)
				{
					Category theCategory			= new Category();
					theCategory.Name				= theRoleName;
					theCategory.CategoryId			= Guid.NewGuid();
					theCategory.SupCategoryId		= theRoleTopCategory.CategoryId;
					theCategory.CreateTime			= DateTime.Now;
					this.m_CategoryManager.Insert(theCategory);
				}
			}
		}

		/// <summary>
		/// check the user whether or not include the role
		/// </summary>
		/// <param name="aUser">the user</param>
		/// <param name="aRole">the role</param>
		/// <returns>Ture or False</returns>
		public bool CheckUserRole(User aUser, ERole aRole)
		{
			return this.CheckUserRole(aUser.UserId, aRole);
		}
		/// <summary>
		/// check the user whether or not include the role
		/// </summary>
		/// <param name="aUserId">the user id</param>
		/// <param name="aRole">the role</param>
		/// <returns>Ture or False</returns>
		public bool CheckUserRole(Guid aUserId, ERole aRole)
		{
			Category theRoleCategory	= this.GetRoleCategoryByName(aRole);
			return ((from theUserRole in m_CosmeticDC.UserRoles
						where theUserRole.UserId == aUserId && theUserRole.RoleCategoryId == theRoleCategory.CategoryId
						select theUserRole).Count() == 1);
		}


		/// <summary>
		/// get ueer role by user role id
		/// </summary>
		/// <param name="aUserRoleId">user role id</param>
		/// <returns>user role</returns>
		public UserRole GetUserRoleById(Guid aUserRoleId)
		{
			List<UserRole> theUserRoleList	= (from theUserRole in m_CosmeticDC.UserRoles
											   where theUserRole.UserRoleId == aUserRoleId
											   select theUserRole).ToList();

			if (theUserRoleList.Count == 1)
			{
				return theUserRoleList[0];
			}
			return null;
		}


		/// <summary>
		/// get all user is contain the role
		/// </summary>
		/// <param name="aRole">the Role</param>
		/// <returns>user list</returns>
		public List<User> GetUserByRole(ERole aRole)
		{
			Category theRoleCategory	= this.GetRoleCategoryByName(aRole);
			return (from theUser in m_CosmeticDC.Users
						join theUserRole in m_CosmeticDC.UserRoles on theUser.UserId equals theUserRole.UserId
						where theUserRole.RoleCategoryId == theRoleCategory.CategoryId
						select theUser).ToList();
		}
		/// <summary>
		/// get all user role is contain the role
		/// </summary>
		/// <param name="aRole">the Role</param>
		/// <returns>user role list</returns>
		public List<UserRole> GetUserRoleByRole(ERole aRole)
		{
			Category theRoleCategory	= this.GetRoleCategoryByName(aRole);
			return (from theUserRole in m_CosmeticDC.UserRoles
						where theUserRole.RoleCategoryId == theRoleCategory.CategoryId
						select theUserRole).ToList();
		}

		/// <summary>
		/// get all roles
		/// </summary>
		/// <returns>all roles lit</returns>
		public List<Category> GetAll()
		{
			Category theRoleTopCategory		= this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Role);
			return (from theCategory in m_CosmeticDC.Categories
					where theCategory.SupCategoryId == theRoleTopCategory.CategoryId && theCategory.Name != theRoleTopCategory.ToString()
					select theCategory).ToList();
		}

	}
}
